package com.mtl.web.controller;

import com.mtl.article.domain.Article;
import com.mtl.article.service.IArticleService;
import com.mtl.front.domain.Sort;
import com.mtl.sort.service.ISortService;
import com.mtl.user.domain.User;
import com.mtl.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.web.firewall.HttpFirewall;
import org.springframework.security.web.firewall.StrictHttpFirewall;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("article")
public class LoginController {
    @Autowired
    IUserService userService;
    @Autowired
    IArticleService articleService;
    @Autowired
    ISortService sortService;
    @RequestMapping("/contact")
    public String loginSend (Model model, HttpServletRequest request) {
        model.addAttribute("user",new User());
        if (request.getSession().getAttribute("name")!=null) {
            return "home";
        }else {
            return "contact";
        }
    }
    @PostMapping("/contact")
    public String loginSubmit(@ModelAttribute User user,Model model ,HttpServletRequest request) {
        String userName=user.getUserName();
        String password=user.getPassword();
        User login = userService.login(userName, password);
        if (login!=null) {
            request.getSession().setAttribute("name",userName);
            System.out.println(login.getUserName());
            return "home";
        }
        else {
            model.addAttribute("error","用户名或密码错误");
            return "contact";
        }
      //  System.out.println("========="+user);

    }
    @RequestMapping("/home")
    public String homeSubmit(@ModelAttribute Article article,  String outLogin, HttpServletRequest request) {
        article.setUserId((Integer) request.getSession().getAttribute("userId"));
        if (article.getText()!=null){
            articleService.saveOrUpdate(article);
        }
        if (outLogin!=null){
            request.getSession().invalidate();
        }
        if (request.getSession().getAttribute("name")!=null) {
            return "home";
        }else {
            return "contact";
        }
    }
    @RequestMapping("/sign")
    public String signSend (Model model){
        model.addAttribute("user",new User());
        return "sign";
    }

    @RequestMapping("/login")
    public String signSubmit(@ModelAttribute User user,HttpServletRequest request) {
        userService.saveOrUpdate(user);
        if (request.getSession().getAttribute("name")!=null) {
            return "home";
        }else {
            return "contact";
        }
    }
    @PostMapping("/verCode")
    @ResponseBody
    public boolean verCOde(@RequestParam String verCode, HttpServletRequest request) {
        HttpSession session=request.getSession();
        StringBuffer checkCode= (StringBuffer)session.getAttribute("checkCode");
        boolean check=false;
        if (verCode.equals(checkCode.toString())) {
            check=true;
        }
        System.out.println( checkCode+"========="+verCode);
        return check;
    }
}
